#include "error.h"
#include "datetimeutils.h"

time_t tm_to_time_t(const struct tm& tm)
{
  time_t ret = mktime((struct tm*)&tm);
  return ret;
}

struct tm time_t_to_tm(time_t t)
{
  struct tm tm;
  localtime_r(&t, &tm);
  return tm;
}

string strftime(const char* fmt, const struct tm& tm)
{
  char buf[1024];
  strftime(buf, sizeof buf, fmt, &tm);
  return string(buf);
}

struct tm strptime(const char* fmt, const char* time_s)
{
  struct tm ret;
  if (::strptime(time_s, fmt, &ret) == 0) {
    throw Error(__FILE__, __LINE__, "strptime failed");
  }
  return ret;
}

